﻿using System;
using System.Web.UI;
using viNews.Classes;
using viNews.Controllers;
using visinia.SmartEngine;
using visinia.SmartEngine.Modules;

public partial class Modules_viNews_BreadCrumb_BreadCrumb : WebModule
{
    private const string CategoryLink = "<a href=\"{0}\" title=\"{1}\">{2}</a> ";
    /*****************************************************************************************/
    // URL = News/Category name.aspx    
    /*****************************************************************************************/
    // URL = News/Category name/SubCategory name.aspx
    /*****************************************************************************************/

    protected override void WebModuleRender(HtmlTextWriter output)
    {
        string data = "";
        string cat = Request.QueryString["CategoryId"];
        string subcat = Request.QueryString["SubCategoryId"];
        string news = Request.QueryString["NewsId"];
        if( ! string.IsNullOrEmpty(cat))
        {
            int catid = 0;
            if(Int32.TryParse(cat, out catid))
            {
                Category category = CategoryController.GetCategory(catid);
                if (category != null)
                {
                    if (category.PageId > 0)// if this category has a specific page to display
                    {
                        string url = WebRoots.GetWebRoot + BaseController.viNewsUrlSeparator + "/" + category.Name + ".aspx";
                        data = string.Format(CategoryLink, url, category.Name, category.Name);
                    }
                    else// this category has no specific assigned page
                    {
                        data = category.Name;
                    }

                    // handle subcategory
                    if (!string.IsNullOrEmpty(subcat))
                    {
                        int subcatid = 0;
                        if (Int32.TryParse(subcat, out subcatid))
                        {
                            Category subcategory = CategoryController.GetCategory(subcatid);
                            if (subcategory != null)
                            {
                                if (subcategory.PageId > 0)// if this subcategory has a specific page to display
                                {
                                    string url = WebRoots.GetWebRoot + BaseController.viNewsUrlSeparator + "/" + category.Name + "/" + subcategory.Name + ".aspx";
                                    data += " | " + string.Format(CategoryLink, url, subcategory.Name, subcategory.Name);
                                }
                                else// this subcategory has no specific assigned page
                                {
                                    data += " | " + subcategory.Name;
                                }
                                // handle news
                                if (!string.IsNullOrEmpty(news))
                                {
                                    int newsid = 0;
                                    if (Int32.TryParse(news, out newsid))
                                    {
                                        News newsdata = NewsController.GetNews(newsid);
                                        if (newsdata != null)
                                        {                                            
                                            data += " | " + newsdata.Title;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        if (!string.IsNullOrEmpty(data))        
            output.Write(data);
    }
}
